<template>
  <div>
    <!-- 面包屑 -->
    <div class="crumbs">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-s-fold"></i> 首页</el-breadcrumb-item>
        <el-breadcrumb-item>救援任务</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <!-- 表单 -->
    <el-card shadow="always">
      <!-- 表头 -->
      <div slot="header">
        <el-form label-width="100px">
          <el-form-item label="订单编号:">
            <el-input type="number" v-model="searchParams.id" placeholder="订单编号:" size="medium"
              style="width: 350px;"></el-input>
          </el-form-item>

          <el-form-item label="救援类型:">
            <el-input v-model="searchParams.type" placeholder="救援类型:" size="medium"
              style="width: 350px;"></el-input>
          </el-form-item>

        </el-form>
        <el-button type="primary" size="medium" @click="searchRescue">筛选</el-button>
        <el-button size="medium" @click="clearSearchParams">清空筛选条件</el-button>
      </div>
      <!-- 表格 -->
      <el-table border style="width: 100%" v-loading="loading" size="medium" :data="tableData">
        <el-table-column prop="id" label="订单编号" align="center">
        </el-table-column>
        <el-table-column prop="rescueby" label="求助人" align="center">
        </el-table-column>
        <el-table-column prop="dicname" label="救援类型" align="center">
        </el-table-column>
        <el-table-column prop="carid" label="车牌" align="center">
        </el-table-column>
        <el-table-column prop="address" label="救援位置" align="center">
        </el-table-column>
        <el-table-column prop="startaddress" label="终点位置" align="center">
        </el-table-column>
        <el-table-column prop="applytime" label="申请时间" align="center">
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination style="text-align: center; margin-top: 20px;" :current-page.sync="pageInfo.pageNum"
        :page-sizes="[3, 5, 10, 20]" :page-size.sync="pageInfo.pageSize" layout="prev, pager, next,total,jumper,->, sizes"
        :total="pageInfo.total" @current-change="getRescue" @size-change="getRescue">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      pageInfo: {
        pageNum: 1,
        pageSize: 3,
        total: 20,
      },
      searchParams: {
        id: '',
        type: '',
      },
      tableData: [], // 空的数据数组
      loading: false,
    }
  },
  mounted() {
    this.getRescue();
  },
  methods: {
    // 获取救援任务列表
    async getRescue() {
      this.loading = true;
      let { pageNum, pageSize } = this.pageInfo;
      let { id, type } = this.searchParams;
      let query = this.$QS.stringify({ type, id, pageNum, pageSize });
      let res = await this.$API.Rescue.reqRescue(query);
      if (res.code == 200) {
        this.pageInfo.total = res.data.total;
        this.tableData = res.data.list;
      } else {
        this.tableData = [];
        this.pageInfo.total = 0;
        this.$message({ message: '没有数据', type: 'error' })
      }
      this.loading = false;
    },
    // 筛选救援任务
    async searchRescue() {
      this.pageInfo.pageNum = 1; // 重置页码为1
      await this.getRescue();
      if (this.tableData.length > 0) {
        this.$message({ message: '筛选成功', type: 'success' })
      }
    },
    // 清空筛选条件
    clearSearchParams() {
      this.searchParams = {
        id: '',
        rescueType: '',
      };
      this.pageInfo.pageNum = 1; // 重置页码为1
      this.getRescue();
    }
  }
}
</script>

<style scoped></style>